// #include "kernel_operator.h"
// constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

// class KernelReciprocal {
// public:
//     __aicore__ inline KernelReciprocal() {}
//     __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t totalLength, uint32_t tileNum)
//     {
//         this->blockLength = totalLength / AscendC::GetBlockNum();
//         this->tileNum = tileNum;
//         this->tileLength = this->blockLength / tileNum / BUFFER_NUM;

//         xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//         zGm.SetGlobalBuffer((__gm__ DTYPE_Z *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
//         pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));
//         pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Z));
//     }
//     __aicore__ inline void Process()
//     {
//         int32_t loopCount = this->tileNum * BUFFER_NUM;
//         for (int32_t i = 0; i < loopCount; i++) {
//             CopyIn(i);
//             Compute(i);
//             CopyOut(i);
//         }
//     }

// private:
//     __aicore__ inline void CopyIn(int32_t progress)
//     {
//         AscendC::LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
//         AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
//         inQueueX.EnQue(xLocal);
//     }
//     __aicore__ inline void Compute(int32_t progress)
//     {
//         AscendC::LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
//         AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.AllocTensor<DTYPE_Z>();
//         float one = -1.0f;
//         AscendC::Power(zLocal, xLocal, one, this->tileLength);
//         outQueueZ.EnQue<DTYPE_Z>(zLocal);
//         inQueueX.FreeTensor(xLocal);
//     }
//     __aicore__ inline void CopyOut(int32_t progress)
//     {
//         AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.DeQue<DTYPE_Z>();
//         AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
//         outQueueZ.FreeTensor(zLocal);
//     }

// private:
//     AscendC::TPipe pipe;
//     AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
//     AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
//     AscendC::GlobalTensor<DTYPE_X> xGm;
//     AscendC::GlobalTensor<DTYPE_Z> zGm;
//     uint32_t blockLength;
//     uint32_t tileNum;
//     uint32_t tileLength;
// };

// extern "C" __global__ __aicore__ void reciprocal_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling)
// {
//     GET_TILING_DATA(tiling_data, tiling);
//     KernelReciprocal op;
//     op.Init(x, z, tiling_data.totalLength, tiling_data.tileNum);
//     op.Process();
// }

#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 2;

template<typename TYPE_X, typename TYPE_Z> class KernelReciprocal {
    using T = TYPE_X;
public:
    __aicore__ inline KernelReciprocal() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ TYPE_Z*)z + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_Z));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.DeQue<TYPE_X>();
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.AllocTensor<TYPE_Z>();

      AscendC::Power(zLocal, xLocal, (TYPE_X)(-1), this->processDataNum);

      outQueueZ.EnQue<TYPE_Z>(zLocal);
      inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Z> zLocal = outQueueZ.DeQue<TYPE_Z>();  
      AscendC::DataCopy(zGm[progress * this->tileDataNum], zLocal, this->processDataNum);
      outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Z> zGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};
extern "C" __global__ __aicore__ void reciprocal_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelReciprocal<DTYPE_X, DTYPE_Z> op;
    op.Init(x, z, tiling_data.smallCoreDataNum, 
            tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
            tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
            tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
            tiling_data.tailBlockNum);  
    op.Process();
}
